#!/usr/bin/env python3
"""
Show complete code content of a node
"""

import pickle

# Load the graph
pkl_file = "/data_ext/ref_code/LocAgent/index_data_strict/pp_data/graph_index_v2.3/business-payout-with-code-content.pkl"

print("Loading graph...")
with open(pkl_file, "rb") as f:
    G = pickle.load(f)

# Find a function node with reasonable size code
target_node = None
for node_id in G.nodes():
    node_data = G.nodes[node_id]
    if (node_data.get('type') == 'function' and
        node_data.get('code') and
        500 < len(node_data['code']) < 2000):  # Medium size function
        target_node = (node_id, node_data)
        break

if target_node:
    node_id, node_data = target_node

    print("=" * 80)
    print(f"NODE: {node_id}")
    print("=" * 80)

    print(f"\n📊 METADATA:")
    print(f"  Type: {node_data.get('type')}")
    print(f"  Start line: {node_data.get('start_line')}")
    print(f"  End line: {node_data.get('end_line')}")
    print(f"  Method calls: {node_data.get('method_calls', [])}")

    code = node_data.get('code', '')
    code_content = node_data.get('code_content', '')

    print(f"\n📏 CODE STATISTICS:")
    print(f"  'code' field: {len(code)} characters, {len(code.split(chr(10)))} lines")
    print(f"  'code_content' field: {len(code_content)} characters, {len(code_content.split(chr(10)))} lines")
    print(f"  Fields identical: {code == code_content}")

    print(f"\n📝 COMPLETE CODE CONTENT:")
    print("─" * 80)
    print(code)
    print("─" * 80)

    # Also show edges for context
    print(f"\n🔗 GRAPH CONNECTIONS:")

    # Incoming edges
    in_edges = list(G.in_edges(node_id, data=True))
    if in_edges:
        print(f"  Incoming ({len(in_edges)} edges):")
        for src, _, data in in_edges[:5]:
            edge_type = data.get('type', 'unknown')
            print(f"    ← {src} [{edge_type}]")

    # Outgoing edges
    out_edges = list(G.out_edges(node_id, data=True))
    if out_edges:
        print(f"  Outgoing ({len(out_edges)} edges):")
        for _, dst, data in out_edges[:5]:
            edge_type = data.get('type', 'unknown')
            print(f"    → {dst} [{edge_type}]")

# Also find a class with complete code
print("\n" + "=" * 80)
print("LOOKING FOR A CLASS WITH COMPLETE CODE")
print("=" * 80)

for node_id in G.nodes():
    node_data = G.nodes[node_id]
    if (node_data.get('type') == 'class' and
        node_data.get('code') and
        1000 < len(node_data['code']) < 3000):

        print(f"\nNODE: {node_id}")
        print(f"Type: {node_data.get('type')}")
        print(f"Lines: {node_data.get('start_line')} - {node_data.get('end_line')}")

        code = node_data.get('code', '')
        print(f"\n📝 COMPLETE CLASS CODE ({len(code)} chars):")
        print("─" * 80)
        print(code)
        print("─" * 80)
        break

print("\n✅ Complete code display finished!")